package com.patrick.open.api.core.common;

/**
 * 响应码
 *
 * @author GP
 * @date 2019/10/21 11:02 AM
 */

public enum RespCode {
  /**
   * 成功
   */
  OK(0000, "success"),
  FAIL(1000, "fail"),
  ALERT(1001, "alert"),

  /**
   * oauth2返回码
   */
  INVALID_TOKEN(2000, "invalid_token"),
  INVALID_SCOPE(2001, "invalid_scope"),
  INVALID_REQUEST(2002, "invalid_request"),
  INVALID_CLIENT(2003, "invalid_client"),
  INVALID_GRANT(2004, "invalid_grant"),
  REDIRECT_URI_MISMATCH(2005, "redirect_uri_mismatch"),
  UNAUTHORIZED_CLIENT(2006, "unauthorized_client"),
  EXPIRED_TOKEN(2007, "expired_token"),
  UNSUPPORTED_GRANT_TYPE(2008, "unsupported_grant_type"),
  UNSUPPORTED_RESPONSE_TYPE(2009, "unsupported_response_type"),
  UNAUTHORIZED(2012, "unauthorized"),
  SIGNATURE_DENIED(2013, "signature_denied"),

  ACCESS_DENIED(4030, "access_denied"),
  ACCESS_DENIED_BLACK_LIMITED(4031, "access_denied_black_limited"),
  ACCESS_DENIED_WHITE_LIMITED(4032, "access_denied_white_limited"),
  ACCESS_DENIED_AUTHORITY_EXPIRED(4033, "access_denied_authority_expired"),
  ACCESS_DENIED_UPDATING(4034, "access_denied_updating"),
  ACCESS_DENIED_DISABLED(4035, "access_denied_disabled"),
  ACCESS_DENIED_NOT_OPEN(4036, "access_denied_not_open"),
  /**
   * 账号错误
   */
  BAD_CREDENTIALS(3000, "bad_credentials"),
  ACCOUNT_DISABLED(3001, "account_disabled"),
  ACCOUNT_EXPIRED(3002, "account_expired"),
  CREDENTIALS_EXPIRED(3003, "credentials_expired"),
  ACCOUNT_LOCKED(3004, "account_locked"),
  USERNAME_NOT_FOUND(3005, "username_not_found"),

  /**
   * 请求错误
   */
  BAD_REQUEST(4000, "bad_request"),
  NOT_FOUND(4004, "not_found"),
  METHOD_NOT_ALLOWED(4005, "method_not_allowed"),
  MEDIA_TYPE_NOT_ACCEPTABLE(4006, "media_type_not_acceptable"),
  TOO_MANY_REQUESTS(4029, "too_many_requests"),
  /**
   * 系统错误
   */
  ERROR(5000, "error"),
  GATEWAY_TIMEOUT(5004, "gateway_timeout"),
  SERVICE_UNAVAILABLE(5003, "service_unavailable"),

  /**
   * 参数错误
   */
  PARAMS_ISNULL(6001, "params_is_null"),
  PARAMS_TYPE_MISMATCH(6001, "param_type_mismatch"),
  PARAMS_ILLEGAL_TYPE(6001, "param_type_mismatch"),

  /**
   * 业务异常
   */
  SERVICE_DEAL_ERROR(7001, "service_deal_error");

  private int code;
  private String msg;

  RespCode() {
  }

  private RespCode(int code, String message) {
    this.code = code;
    this.msg = message;
  }

  public static RespCode getResultEnum(int code) {
    for (RespCode type : RespCode.values()) {
      if (type.getCode() == code) {
        return type;
      }
    }
    return ERROR;
  }

  public static RespCode getResultEnum(String message) {
    for (RespCode type : RespCode.values()) {
      if (type.getMsg().equals(message)) {
        return type;
      }
    }
    return ERROR;
  }


  public int getCode() {
    return code;
  }

  public String getMsg() {
    return msg;
  }


}
